package com.rjwh.dingdong.client.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.TopicFavoriteListData;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.MyGridView;
import com.rjwh.dingdong.client.widget.RoundImageView;
import com.wby.base.AdapterBase;

import java.util.List;

/**
 * @author Kay
 * @ClassName TopicFavoriteAdapter
 * @Description
 * @date 2016-7-21
 * @history 2016-7-21 author: description:
 */
public class TopicFavoriteAdapter extends AdapterBase<TopicFavoriteListData> {

    private Context context;
    private int screen;

    public TopicFavoriteAdapter(Context context) {
        this.context = context;
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width_screen = dm.widthPixels;
        screen = (int) Math.round(width_screen * 0.75);
    }

    @Override
    protected View getExView(int i, View view, ViewGroup viewGroup) throws Exception {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.topic_favorite_list_item, null);
        }
        RoundImageView imageView = ViewHolder.get(view, R.id.topic_favorite_item_img_view);
        ImageView levelImg = ViewHolder.get(view, R.id.topic_favorite_item_user_level_img);
        TextView contentTv = ViewHolder.get(view, R.id.topic_favorite_item_content);
        TextView nameTv = ViewHolder.get(view, R.id.topic_favorite_item_user_name);
        TextView timeTv = ViewHolder.get(view, R.id.topic_favorite_item_send_time);
        TextView postionTv = ViewHolder.get(view, R.id.topic_favorite_item_position_tv);
        MyGridView picGV = ViewHolder.get(view, R.id.topic_favorite_item_pic_gridview);

        TopicFavoriteListData topicFavoriteListData = mList.get(i);
        String avatar = topicFavoriteListData.getAvatar();
        String name = topicFavoriteListData.getName();
        String title = topicFavoriteListData.getTitle();
        String time = topicFavoriteListData.getTime();
        String region = topicFavoriteListData.getRegion();
        String userlevel = topicFavoriteListData.getUserlevel();
        List<String> image = topicFavoriteListData.getImage();

        if (avatar != null) {
            Glide.with(context).load(avatar).dontAnimate().placeholder(R.drawable.icon_defult_avatar).error(R.drawable.icon_defult_avatar).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
        }
        if (!AsStrUtils.isEmpty(title)) {
            contentTv.setText(title);
        }
        if (!AsStrUtils.isEmpty(name)) {
            nameTv.setText(name);
        }
        if (!AsStrUtils.isEmpty(userlevel)) {
            setLevelIcon(levelImg, Integer.parseInt(userlevel));
        }
        if (!AsStrUtils.isEmpty(time)) {
            timeTv.setText(time);
        }
        if (!AsStrUtils.isEmpty(region)) {
            postionTv.setText(region);
        }
        if (image != null) {
            MyPicAdapter adapter = new MyPicAdapter(context, image, null, screen);
            // 设置GridView不抢夺焦点
            picGV.setClickable(false);
            picGV.setPressed(false);
            picGV.setEnabled(false);
            picGV.setAdapter(adapter);
        }

        return view;
    }

    @Override
    protected void onReachBottom() {

    }

    private void setLevelIcon(ImageView imageView, int level) {
        int id = 0;
        switch (level) {
            case 1:
                id = R.drawable.icon_level_v1;
                break;
            case 2:
                id = R.drawable.icon_level_v2;
                break;
            case 3:
                id = R.drawable.icon_level_v3;
                break;
            case 4:
                id = R.drawable.icon_level_v4;
                break;
            case 5:
                id = R.drawable.icon_level_v5;
                break;
            case 6:
                id = R.drawable.icon_level_v6;
                break;
            default:
                break;
        }
        imageView.setImageResource(id);
    }
}
